package com.romadan.core.wolfram.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.romadan.core.nonlinearanalys.fet.logic.SimulationPoint;
import com.wolfram.jlink.Expr;

public class Utils {
	/**
	 * Create List<String> from Wolfram list.
	 * 
	 * @param data
	 *            - Example: {1,2,3}
	 * 
	 * @return List<String>
	 */
	public List<String> convertFromWolframArrayToList(Expr data) {
		List<String> result = new ArrayList<String>();
		String sData = data.head().toString();
		sData = sData.substring(1, sData.length() - 1);
		String[] arData = sData.split(", ");
		for (String a : arData) {
			result.add(a);
		}
		return result;
	}

	/**
	 * Create array for Wolfram. Example: {1,2,3}
	 * 
	 * @param data
	 *            - list for array 1,2,3
	 * 
	 * @return String values of Wolfram array
	 */
	public String convertToWolframArray(ArrayList<String> data) {
		String result = "{";
		for (String str : data) {
			result += str + ", ";
		}
		result = result.substring(0, result.length() - 2) + "}";
		return result;
	}

	/**
	 * Parse result from Wolfram Kernel and returns value of parameter in
	 * Rule[Vpk0, 0.123]
	 * 
	 * @param data
	 *            - result from Wolfram.
	 * @return HashMap<String, String> values of parameters
	 */
	public Map<String, String> convertFromRuleToMap(String data) {
		if (!data.contains("Rule["))
			return null;
		Map<String, String> result = new HashMap<String, String>();
		String[] dataArr = data.split("Rule");
		for (String st : dataArr) {
			if (st.startsWith("[")) {
				st = cleanEndOfStringSequence(st, "]");
				String[] stArr = st.split(", ");
				if (stArr.length == 2 && stArr[0].startsWith("[")) {
					String key = stArr[0].substring(1);
					String value = stArr[1].substring(0, stArr[1].length() - 1);
					result.put(key, value);
				}
			}
		}
		return result;
	}

	public Map<String, Double> convertFromRuleToMapDouble(String data) {
		Map<String, String> sMap = convertFromRuleToMap(data);
		Map<String, Double> result = new HashMap<String, Double>();

		Set<String> keys = sMap.keySet();
		for (String key : keys) {
			Double val = Double.valueOf(sMap.get(key));
			result.put(key, val);
		}
		return result;
	}

	/**
	 * Parse result from Wolfram Kernel and returns value of parameter in
	 * Rule[Vpk0, 0.123]
	 * 
	 * @param data
	 *            - result from Wolfram. arg - name of value.
	 * @return String value of parameters
	 */
	public String getValueFromArrRule(String data, String arg) {
		if (data == null)
			return null;
		return convertFromRuleToMap(data).get(arg);
	}

	public String convertToWolframArrayOfVgs(
			List<SimulationPoint> listSimulationPoints) {
		String result = "{";
		for (SimulationPoint point : listSimulationPoints) {
			result += "{" + point.getVgs() + "," + point.getValue() + "}, ";
		}
		result=result.substring(0, result.length()-2);
		result += "}";
		return result;
	}

	private String cleanEndOfStringSequence(String data, String lastChar) {
		if (data.contains(lastChar)) {
			while (!data.endsWith(lastChar)) {
				data = data.substring(0, data.length() - 1);
			}
		}
		return data;
	}
}
